/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.domain.IdentityItem;
import me.zhengjie.modules.system.repository.IdentityItemRepository;
import me.zhengjie.modules.system.service.IdentityItemService;
import me.zhengjie.modules.system.service.dto.IdentityItemDto;
import me.zhengjie.modules.system.service.dto.IdentityItemQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.IdentityItemMapper;
import me.zhengjie.utils.*;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author 王宇
* @date 2023-09-24
**/
@Service
@RequiredArgsConstructor
public class IdentityItemServiceImpl implements IdentityItemService {

    private final IdentityItemRepository identityItemRepository;
    private final IdentityItemMapper identityItemMapper;

    @Override
    public Map<String,Object> queryAll(IdentityItemQueryCriteria criteria, Pageable pageable){
        Page<IdentityItem> page = identityItemRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(identityItemMapper::toDto));
    }

    @Override
    public List<IdentityItemDto> queryAll(IdentityItemQueryCriteria criteria){
        return identityItemMapper.toDto(identityItemRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public IdentityItemDto findById(Long itemId) {
        IdentityItem identityItem = identityItemRepository.findById(itemId).orElseGet(IdentityItem::new);
        ValidationUtil.isNull(identityItem.getItemId(),"IdentityItem","itemId",itemId);
        return identityItemMapper.toDto(identityItem);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public IdentityItemDto create(IdentityItem resources) {
        return identityItemMapper.toDto(identityItemRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(IdentityItem resources) {
        IdentityItem identityItem = identityItemRepository.findById(resources.getItemId()).orElseGet(IdentityItem::new);
        ValidationUtil.isNull( identityItem.getItemId(),"IdentityItem","id",resources.getItemId());
        identityItem.copy(resources);
        identityItemRepository.save(identityItem);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long itemId : ids) {
            identityItemRepository.deleteById(itemId);
        }
    }

    @Override
    public void download(List<IdentityItemDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (IdentityItemDto identityItem : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("编号", identityItem.getItemNo());
            map.put("标题", identityItem.getTitle());
            map.put("副标题", identityItem.getSubTitle());
            map.put("图片", identityItem.getItemPic());
            map.put("售价", identityItem.getSalePrice());
            map.put("折扣价", identityItem.getDiscountPrice());
            map.put("介绍", identityItem.getItemDesc());
            map.put("创建时间", identityItem.getCreateTime());
            map.put("创建人", identityItem.getCreateby());
            map.put("更新时间", identityItem.getUpdateTime());
            map.put("更新人", identityItem.getUpdateby());
            map.put("状态，0：无效，1：有效，-1:删除", identityItem.getStatus());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public IdentityItem findOne(String itemNo) {
        if (StringUtils.isBlank(itemNo)){
            return null;
        }
        IdentityItemQueryCriteria criteria = new IdentityItemQueryCriteria();
        criteria.setItemNo(itemNo);
        List<IdentityItem> all = identityItemRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));
        if (CollectionUtils.isEmpty(all)){
            return null;
        }
        return all.get(0);
    }
}